require "wpb/version"
require "wpb/user"
require "wpb/setting"
require "wpb/pagepost"
require "wpb/page"
require "wpb/post"
require "wpb/comment"
require "active_record"

##
# The wrapper for the wpb gem

module WPB
	class AddTypeToWpPosts < ActiveRecord::Migration
		def self.up
			suppress_messages do
				unless column_exists? :wp_posts, :type
					add_column :wp_posts, :type, :string
					PagePost.reset_column_information
				else
				  puts "\tWordPress Database Already Initialized"
				end
			end
		end

	  def self.down
	    remove_column :wp_posts, :type
	  end
	end
	
	class App
	  def self.connect options = {}
	    config = {
				:adapter  => "mysql", 
				:host     => "127.0.0.1",
				:username => "root",
				:password => "root",
				:database => "wordpress"
			}.merge!(options)

			ActiveRecord::Base.establish_connection(config)
    end
	  
		def self.run options = {}
	  	self.connect options
      if PagePost.attribute_method? :type
  			PagePost.all.each do |p|
  				p.type = p.post_type.capitalize
  				if p.post_type != "post" && p.post_type != "page"
  					p.type = "Post"
  				end
  				p.save
  			end
      else
        puts "Is this the first time you have used this? Try `wpb init`"
        exit(0)
      end
	  end
	end
end
